package com.hxl.demo.controller;

import com.hxl.demo.entity.TrainingClassification;
import com.hxl.demo.entity.TrainingContents;
import com.hxl.demo.service.Impl.TrainingClassificationServiceImpl;
import com.hxl.demo.service.Impl.TrainingContentsServiceImpl;
import com.hxl.demo.service.Impl.TrainingRecodeServiceImpl;

import com.hxl.demo.service.TrainingClassificationService;
import com.hxl.demo.tools.Common;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class TrainingClassificationController {
    @Resource
    private TrainingClassificationServiceImpl trainingClassificationImpl;

    @Resource
    private TrainingContentsServiceImpl trainingContentsService;

    @Resource
    private TrainingRecodeServiceImpl TrainingRecodeServiceImpl;

    @RequestMapping("/getTrainingClassificationList")
    public Object getTrainingClassificationList(HttpServletRequest request){
        HttpSession session = request.getSession();
        int times=0;
        Integer userId = (Integer) session.getAttribute("userInfo");

        List<TrainingClassification> list=trainingClassificationImpl.getTrainingClassificationList();
        for(TrainingClassification trainingClassification:list){
            List<TrainingContents>  trainingContentsList=trainingContentsService.getTrainingContents(trainingClassification.getId());

            for(TrainingContents trainingContents:trainingContentsList){
                if(userId!=null){

                    List<Map<String,Object>> trainingRecord = TrainingRecodeServiceImpl.getRecodeById(userId, trainingContents.getId());
                    if(trainingRecord.size()>0){
                        times= (int) trainingRecord.get(0).get("cumulative_time");
                    }

                }
                trainingContents.setCumulativeTime(times);
                times=0;
            }
            trainingClassification.setContents(trainingContentsList);
        }

        return Common.returnJsonSuccess("操作成功",200,list);

    }
    @RequestMapping("/TrainingClassification")
    public Object getTrainingClassificationList(){
        List<TrainingClassification> list=trainingClassificationImpl.getTrainingClassificationList();
        return  Common.returnJsonSuccess("操作成功",200,list);
    }

}
